package com.xdcplus.web.service.sys.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.github.pagehelper.PageInfo;
import com.xdcplus.mom.constants.NumberConstant;
import com.xdcplus.mom.enums.ResponseEnum;
import com.xdcplus.mp.service.impl.BaseServiceImpl;
import com.xdcplus.mp.utils.PageableUtils;
import com.xdcplus.mp.utils.PropertyUtils;
import com.xdcplus.pager.vo.PageVO;
import com.xdcplus.web.common.pojo.dto.sys.SysBaseDataFilterDTO;
import com.xdcplus.web.common.pojo.entity.sys.SysBaseData;
import com.xdcplus.web.common.pojo.query.sys.SysBaseDataQuery;
import com.xdcplus.web.common.pojo.vo.sys.SysBaseDataVO;
import com.xdcplus.web.mapper.sys.SysBaseDataMapper;
import com.xdcplus.web.service.sys.SysBaseDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 基础参数(SysBaseData)表服务实现类
 *
 * @author bullion.yan
 * @since 2024-06-18 14:00:40
 */
@Slf4j
@Service("sysBaseDataService")
public class SysBaseDataServiceImpl extends BaseServiceImpl<SysBaseDataMapper, SysBaseData, SysBaseData, SysBaseDataVO> implements SysBaseDataService {

    @Autowired
    protected SysBaseDataMapper sysBaseDataMapper;


    private List<SysBaseData> querySysBaseDataList(SysBaseDataFilterDTO sysBaseDataFilterDTO) {
        SysBaseDataQuery sysBaseDataQuery = BeanUtil.copyProperties(sysBaseDataFilterDTO, SysBaseDataQuery.class);

        return sysBaseDataMapper.querySysBaseData(sysBaseDataQuery);
    }

    @Override
    public List<SysBaseDataVO> querySysBaseDataVOList(SysBaseDataFilterDTO sysBaseDataFilterDTO) {
        return this.objectConversion(querySysBaseDataList(sysBaseDataFilterDTO));
    }

    @Override
    public PageVO<SysBaseDataVO> querySysBaseData(SysBaseDataFilterDTO sysBaseDataFilterDTO) {
        PageVO<SysBaseDataVO> pageVO = new PageVO<>();

        if (sysBaseDataFilterDTO.getCurrentPage() > NumberConstant.ZERO) {
            PageableUtils.basicPage(sysBaseDataFilterDTO.getCurrentPage(), sysBaseDataFilterDTO.getPageSize(),
                    sysBaseDataFilterDTO.getOrderType(), sysBaseDataFilterDTO.getOrderField());
        }

        List<SysBaseData> sysBaseDataList = querySysBaseDataList(sysBaseDataFilterDTO);

        PageInfo<SysBaseData> pageInfo = new PageInfo<>(sysBaseDataList);
        PropertyUtils.copyProperties(pageInfo, pageVO, this.objectConversion(sysBaseDataList));

        return pageVO;
    }

    @Override
    public SysBaseDataVO querySysBaseDataById(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        return this.objectConversion(this.getById(id));
    }

    @Override
    public List<SysBaseData> getByDataCate(String dataCate) {
        return sysBaseDataMapper.querySysBaseDataByDataCate(dataCate);
    }

    @Override
    public Map<String, String> getCodeMapByDataCate(String dataCate) {
        List<SysBaseData> sysBaseDataList = this.getByDataCate(dataCate);
        if (CollUtil.isNotEmpty(sysBaseDataList)) {
            return sysBaseDataList.stream()
                    .filter(entity -> entity.getDataId() != null)
                    .collect(Collectors.toMap(SysBaseData::getDataId, SysBaseData::getDataDesc));
        } else {
            return new HashMap<>();
        }
    }

    public Map<String, String> getDescMapByDataCate(String dataCate) {
        List<SysBaseData> sysBaseDataList = this.getByDataCate(dataCate);
        if (CollUtil.isNotEmpty(sysBaseDataList)) {
            return sysBaseDataList.stream()
                    .filter(entity -> entity.getDataDesc() != null)
                    .collect(Collectors.toMap(SysBaseData::getDataDesc, SysBaseData::getDataId));
        } else {
            return new HashMap<>();
        }
    }

    @Override
    public SysBaseData getByDataCateAndDataId(String dataCate, String dataId) {
        return sysBaseDataMapper.querySysBaseDataByDataCateAndDataId(dataCate, dataId);

    }
}
